package org.scct.chenhui.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    //5.7驱动字符串
    private static String dbDriver="com.mysql.jdbc.Driver";
    //8.x驱动字符串
//    private static String dbDriver="com.mysql.cj.jdbc.Driver";
    //5.7连接字符串
    private static String dbUrl = "jdbc:mysql://localhost:3306/librarymanager?characterEncoding=UTF-8&useSSL=false";
    //8.x连接字符串
//    private static String dbUrl = "jdbc:mysql://localhost:3306/librarymanager?characterEncoding=UTF-8&useSSL=FALSE";

    private static String username="root";
    private static String password="it";

    /**
     * 获取数据库连接对象
     * @return 数据连接对象connection
     */
    public static Connection getConnection()
    {
        Connection connection=null;
//        加载驱动
        try {
            Class.forName(dbDriver);
            connection = DriverManager.getConnection(dbUrl, username, password);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return connection;
    }

    public static void closeConnection(Connection connection)
    {
        try {
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
